package com.android.myqimc;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class AlarmReceiver extends BroadcastReceiver {

	private String mcdate_value = "";
	  private String period_value = "";
	  private boolean isExit = true;

	  @Override
	  public void onReceive(Context arg0, Intent arg1)
	  {
	    // TODO Auto-generated method stub
	    //先获取文件里的设置值
	    getFileDate(arg0);
	    //有获取设置值才可运行
	    if (isExit)
	    {
	      //预计下次MC日期
	      String nDate = DateUtil.getNextDate(mcdate_value, Integer
	          .parseInt(period_value), "yyyy/MM/dd");
	      //距离现在还有N天
	      int days = DateUtil.computerDiffDate(DateUtil
	          .getDateTime(DateUtil.getNextDate(mcdate_value, Integer
	              .parseInt(period_value), "yyyyMMdd")), System
	          .currentTimeMillis());
	      String msg = arg0.getResources().getString(
	          R.string.strMessage2);
	      msg += nDate;
	      msg += "\n";
	      if (days >= 0)
	      {
	        msg += arg0.getResources().getString(R.string.strMessage5);
	        msg += days;
	        msg += arg0.getResources().getString(R.string.strMessage7);
	      } else
	      {
	        msg += arg0.getResources().getString(R.string.strMessage8);
	        //取绝对值
	        msg += Math.abs(days);
	        msg += arg0.getResources().getString(R.string.strMessage7);
	      }

	      showToast(arg0, msg);
	      showNotification(arg0,msg);
	    }
	  }

	  private void getFileDate(Context arg0)
	  {
	    String msg = "";
	    Properties p = new Properties();
	    try
	    {
	      p.load(arg0.openFileInput(MainActivity.fileName));
	      mcdate_value = p.getProperty(MainActivity.mcdate_key);
	      period_value = p.getProperty(MainActivity.period_key);
	    } catch (FileNotFoundException e)
	    {
	      isExit = false;
	      msg = e.getMessage();
	      e.printStackTrace();
	    } catch (IOException e)
	    {
	      isExit = false;
	      msg = e.getMessage();
	      e.printStackTrace();
	    }
	    if (msg.length() > 0)
	    {
	      showToast(arg0, msg);
	      showNotification(arg0,msg);
	    }
	  }

	  private void showToast(Context arg0, String msg)
	  {
	    Toast.makeText(arg0, msg, Toast.LENGTH_LONG).show();
	  }
	  
	  private void showNotification(Context arg0,String msg){
		  //创建notification对象，设置显示的icon、状态栏显示的文本、显示的时间（立刻显示）
		  Notification notification = new Notification(
				  R.drawable.ic_notification,"MC提醒",System.currentTimeMillis());
		  //点击该通知栏
		  PendingIntent contentIntent = PendingIntent.getActivity(
				  arg0, 0, new Intent(arg0,MainActivity.class), 0);
		  //下拉显示的信息
		  notification.setLatestEventInfo(arg0, "下次MC", msg, contentIntent);
		  //设置通知管理器对象
		  NotificationManager nm = (NotificationManager) arg0.getSystemService(
				  Context.NOTIFICATION_SERVICE);
		  //删除之前的
		  nm.cancel(0);
          //显示当前的通知信息
		  nm.notify(0, notification);
	  }
}
